package algotradingfx.data;

import java.io.BufferedReader;
import java.io.Reader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import atp.commons.util.Security;
import atp.commons.util.Tick;

public class TickReader extends BufferedReader {
	// 1288552933661 EURUSD MOCK 1.00000 1000 1.50000 1500 5000.00000
	private static final String PATTERN = "^([0-9]+)\\t([0-9:]+)\\t([A-Z]+)\\t([0-9\\.]+)\\t([0-9]+)\\t([0-9\\.]+)\\t([0-9]+)\\t[0-9\\.]+$";
	private static final Pattern pattern = Pattern.compile(PATTERN);

	public Tick toTick(String s) {
		if (s == null)
			return null;
		Matcher m = pattern.matcher(s);
		if (m.matches()) {
			return new Tick(new Security(m.group(3), m.group(3)), Double
					.parseDouble(m.group(4)), Double.parseDouble(m.group(6)),
					Long.parseLong(m.group(5)), Long.parseLong(m.group(7)),
					Long.parseLong(m.group(1)));
		}
		return null;
	}

	public TickReader(Reader reader) {
		super(reader);
	}
}
